package com.litxuf.util;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLOperate {
	// 修改xml
//	static String path = ClassLoader.getSystemResource(FinalInformation.XMLFILENAME).getPath();// 获得xml文件的绝对路径
	static Logger logger = Logger.getLogger(XMLOperate.class.getName());
 

	public static boolean updateXML( String path,String value, String elePath) {
		try {
			// 创建读入对象
			SAXReader reader = new SAXReader();
			// 创建document实例
			Document doc = reader.read(new File(path));
			List listText = doc.selectNodes(elePath);
			Iterator itText = listText.iterator();
			while (itText.hasNext()) {
				Element textEle = (Element) itText.next();
				// name节点的文本内容修改为value
				textEle.setText(value);
			}
			XMLWriter output = new XMLWriter(new FileWriter(new File(path)));
			output.write(doc);
			output.close();
			logger.info("---更新 xml 成功！最后添加的索引ID是:"+value+"----");
			return true;
		} catch (Exception e) {
			logger.info("修改xml文件出错" + ExceptionUtils.getFullStackTrace(e));
		}
		return false;
	}

	public static boolean updateXMLForDeleteIDS(String path,String value, String elePath,
			boolean flag) {
		try {
			// 创建读入对象
			SAXReader reader = new SAXReader();
			// 创建document实例
			Document doc = reader.read(new File(path));
			List listText = doc.selectNodes(elePath);
			Iterator itText = listText.iterator();
			while (itText.hasNext()) {
				Element textEle = (Element) itText.next();

				if (flag) {// 追加需要删除的id
					String newValue = textEle.getText() + "," + value;
					// name节点的文本内容修改为value
					textEle.setText(newValue);
				} else {// 索引删除成功
					textEle.setText(value);
				}
			}
			XMLWriter output = new XMLWriter(new FileWriter(new File(path)));
			output.write(doc);
			output.close();
			logger.info("更新 xml 即(删除索引IDS) ID 成功");
			return true;
		} catch (Exception e) {
			logger.info("修改xml文件(删除索引IDS)出错"
					+ ExceptionUtils.getFullStackTrace(e));
		}
		return false;
	}

	// 解析xml
	public static String readXML(String path,String elePath) {
		try {
			// 创建读入对象
			SAXReader reader = new SAXReader();
			// 创建document实例
			Document doc = reader.read(new File(path));
			List listText = doc.selectNodes(elePath);
			String code = "";
			Iterator itText = listText.iterator();
			while (itText.hasNext()) {
				Element textEle = (Element) itText.next();
				// name节点的文本内容修改为value
				code = textEle.getText();
			}
			logger.info("读取 xml 成功 ID="+code);
			return code;
		} catch (Exception e) {
			logger.info("读取xml文件出错" + ExceptionUtils.getFullStackTrace(e));
		}
		return null;
	}

	public static int readXMLToInt(String path,String elePath) {
		try {
			// 创建读入对象
			SAXReader reader = new SAXReader();
			// 创建document实例
			Document doc = reader.read(new File(path));
			List listText = doc.selectNodes(elePath);
			String code = "";
			Iterator itText = listText.iterator();
			while (itText.hasNext()) {
				Element textEle = (Element) itText.next();
				code = textEle.getText();
			}
			logger.info("读取 xml 成功 ID="+code);
			return Integer.parseInt(code);
		} catch (Exception e) {
			logger.info("读取xml文件出错" + ExceptionUtils.getFullStackTrace(e));
		}
		return -1;
	}

}
